package development;

import iplayparticle.view.iplayparticleResources;
import state.DrawListener;
import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.SMSHandler;
import state.SendSMSListener;
import state.Sprite;
import state.TouchScreenListener;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.array.StringArray;
import development.socket.SocketClientUtil;

public class FrmRegister extends GameForm implements SendSMSListener, TouchScreenListener {
  private GameImage buttonImage;
  private ImageText text8;

  private int chooseIndex;

  private TextField userNameTextField;
  private TextField passwordTextField;
  private TextField passwordTextField1;
  private Sprite registerButtonSprite;

  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmRegister();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);

    userNameTextField = new TextField(_manager, TextField.CONSTRAINT_ANY, Constants.TEXT_FIELD_STYLE);
    passwordTextField = new TextField(_manager, TextField.CONSTRAINT_PASSWORD, Constants.TEXT_FIELD_STYLE);
    passwordTextField1 = new TextField(_manager, TextField.CONSTRAINT_PASSWORD, Constants.TEXT_FIELD_STYLE);
    buttonImage = new GameImage(iplayparticleResources.image_button, null, null);

    if (GameConstants.IS_240x320_SCREEN) {
      userNameTextField.setPosition(70, 75);
      userNameTextField.setSize(150, 20);
      passwordTextField.setPosition(70, 115);
      passwordTextField.setSize(150, 20);
      passwordTextField1.setPosition(70, 155);
      passwordTextField1.setSize(150, 20);
      registerButtonSprite = new Sprite(buttonImage, _manager, 85, 195, Constants.TOP_LEFT_ANCHOR);
    } else {
      userNameTextField.setPosition(128, 74);
      userNameTextField.setSize(150, 20);
      passwordTextField.setPosition(128, 103);
      passwordTextField.setSize(150, 20);
      passwordTextField1.setPosition(128, 132);
      passwordTextField1.setSize(150, 20);
      registerButtonSprite = new Sprite(buttonImage, _manager, 145, 162, Constants.TOP_LEFT_ANCHOR);
    }
    
    registerButtonSprite.setTouchScreenListener(this);
    registerButtonSprite.setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        if (chooseIndex == 3) {
          text8.drawString2(g, "Đăng ký", GameColor.YELLOW_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
        } else {
          text8.drawString2(g, "Đăng ký", GameColor.WHITE_CODE, source.getRealX() + source.getWidth() / 2, source.getRealY() + 2, Constants.TOP_HCENTER_ANCHOR);
        }
      }
    });

    chooseIndex = 0;// up first
    updateFocus();
    DrawUtils.setBottomBar(_manager, "Trở về", "", "Xóa");
  }

  private void drawFor240x320(Graphics g) {
    text8.drawString2(g, "Tài khoản", GameColor.WHITE_CODE, 12, 75, GameConstants.TOP_LEFT_ANCHOR);
    userNameTextField.draw(g);
    text8.drawString2(g, "Mật khẩu", GameColor.WHITE_CODE, 12, 115, GameConstants.TOP_LEFT_ANCHOR);
    passwordTextField.draw(g);
    text8.drawString2(g, "Nhập lại", GameColor.WHITE_CODE, 12, 145, GameConstants.TOP_LEFT_ANCHOR);
    text8.drawString2(g, "mật khẩu", GameColor.WHITE_CODE, 12, 160, GameConstants.TOP_LEFT_ANCHOR);
    passwordTextField1.draw(g);
  }

  private void drawFor320x240(Graphics g) {
    text8.drawString2(g, "Tài khoản", GameColor.WHITE_CODE, 52, 76, GameConstants.TOP_LEFT_ANCHOR);
    userNameTextField.draw(g);
    text8.drawString2(g, "Mật khẩu", GameColor.WHITE_CODE, 52, 105, GameConstants.TOP_LEFT_ANCHOR);
    passwordTextField.draw(g);
    text8.drawString2(g, "Nhập lại", GameColor.WHITE_CODE, 52, 124, GameConstants.TOP_LEFT_ANCHOR);
    text8.drawString2(g, "mật khẩu", GameColor.WHITE_CODE, 52, 139, GameConstants.TOP_LEFT_ANCHOR);
    passwordTextField1.draw(g);
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    if (GameConstants.IS_240x320_SCREEN) {
      drawFor240x320(g);
    } else {
      drawFor320x240(g);
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.ENTER:
      if (chooseIndex == 0) {
        userNameTextField.pointerReleased(userNameTextField, userNameTextField.getRealX(), userNameTextField.getRealY());
      } else if (chooseIndex == 1) {
        passwordTextField.pointerReleased(passwordTextField, passwordTextField.getRealX(), passwordTextField.getRealY());
      } else if (chooseIndex == 2) {
        passwordTextField1.pointerReleased(passwordTextField1, passwordTextField1.getRealX(), passwordTextField1.getRealY());
      } else if (chooseIndex == 3) {
        registerBySMS();
      } 
      break;
    case Key.UP:
      chooseIndex--;
      if (chooseIndex < 0) {
        chooseIndex = 3;
      }
      updateFocus();
      break;
    case Key.DOWN:
      chooseIndex++;
      if (chooseIndex > 3) {
        chooseIndex = 0;
      }
      updateFocus();
      break;
    case Key.SOFT_LEFT:
      GameGlobal.nextState(FrmLogin.instance(), null);
      break;
    case Key.SOFT_RIGHT:
    case Key.BACKSPACE:
      if (chooseIndex == 0) {
        userNameTextField.deleteLastChar();
      } else if (chooseIndex == 1) {
        passwordTextField.deleteLastChar();
      } else if (chooseIndex == 2) {
        passwordTextField1.deleteLastChar();
      }
      break;
    default:
      if (chooseIndex == 0) {
        userNameTextField.keyReleased1(keyCode);
      } else if (chooseIndex == 1) {
        passwordTextField.keyReleased1(keyCode);
      } else if (chooseIndex == 2) {
        passwordTextField1.keyReleased1(keyCode);
      }
      break;
    }
  }

  private void registerBySMS() {
    String username = userNameTextField.getText();
    String password = passwordTextField.getText();
    String password1 = passwordTextField1.getText();

    if (username.length() < Constants.USER_NAME_MIN_LEN) {
      GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Tên đăng nhập phải", "dài hơn " + Constants.USER_NAME_MIN_LEN + " ký tự" ));
      return;
    }

    if (username.length() > Constants.USER_NAME_MAX_LEN) {
      GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Tên đăng nhập phải", "ngắn hơn " + Constants.USER_NAME_MAX_LEN + " ký tự" ));
      return;
    }

    if (!password.equals(password1)) {
      GameGlobal.alert.showAlert(this, GameAlert.OK_TYPE, "Mật khẩu nhập lại không đúng" );
      return;
    }

    if (password.length() < Constants.PASSWORD_MIN_LEN) {
      GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Mật khẩu phải", "dài hơn " + Constants.PASSWORD_MIN_LEN + " ký tự" ));
      return;
    }

    if (password.length() > Constants.PASSWORD_MAX_LEN) {
      GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Mật khẩu phải", "ngắn hơn " + Constants.PASSWORD_MAX_LEN + " ký tự" ));
      return;
    }
    
    // Show loading alert
    Global.alert.showAlert(this, GameAlert.LOADING_WITH_NO_BUTTON_TYPE, "Đang đăng ký...").setAlertTimeOut(30000);
    
    // 7x85
    StringArray sms = (StringArray) Global.smsCommand.get("GAME_DK");
    if (sms == null || sms.len() == 0) {
      SocketClientUtil.register(username, password);
    } else {
      StringBuffer smsCommand = new StringBuffer();
      smsCommand.append(sms.array[0]);
      smsCommand.append(" ");
      smsCommand.append(username);
      smsCommand.append(" ");
      smsCommand.append(password);
      smsCommand.append(" ");
      smsCommand.append(Global.provider.getId());
      if(Global.provider.getId() == Constants.TINHVAN_PROVIDER_ID){
        SMSHandler.getInstance(this).sendMsg(sms.array[1], Constants.SMS_PREFIX +" "+ smsCommand.toString());
      }else if(Global.provider.getId() == Constants.HDC_PROVIDER_ID){
        SMSHandler.getInstance(this).sendMsg(sms.array[1],  smsCommand.toString() +" "+ Constants.CARD_SECURE_KEY);
      }else{
        SMSHandler.getInstance(this).sendMsg(sms.array[1], smsCommand.toString());
      }
    }
  }

  public void updateFocus() {
    userNameTextField.setFocused(false);
    passwordTextField.setFocused(false);
    passwordTextField1.setFocused(false);
    if (chooseIndex == 0) {
      userNameTextField.setFocused(true);
      DrawUtils.setBottomBar(_manager, "Trở về", "", "Xóa");
    } else if (chooseIndex == 1) {
      passwordTextField.setFocused(true);
      DrawUtils.setBottomBar(_manager, "Trở về", "", "Xóa");
    } else if (chooseIndex == 2) {
      passwordTextField1.setFocused(true);
      DrawUtils.setBottomBar(_manager, "Trở về", "", "Xóa");
    } else if (chooseIndex == 3) {
      DrawUtils.setBottomBar(_manager, "Trở về", "Đăng ký", "");
    }
  }

  public void onSendSMSFail() {
    Global.alert.showAlert(this, GameAlert.OK_TYPE, "Gửi tin nhắn đăng ký thất bại");
  }

  public void onSendSMSSuccess() {
    Global.alert.showAlert(this, GameAlert.OK_TYPE, "Gửi tin nhắn thành công");
  }

  public void destroy() {
    text8 = null;
    buttonImage = null;
    registerButtonSprite = null;

    userNameTextField = null;
    passwordTextField = null;
    passwordTextField1 = null;
  }

  public void pointerDragged(Sprite source, int x, int y) {
  }

  public void pointerPressed(Sprite source, int x, int y) {
  }

  public void pointerReleased(Sprite source, int x, int y) {
    if (registerButtonSprite == source) {
      chooseIndex = 3;
      updateFocus();
      keyReleased(Key.FIRE);
    }
  }
}
